var USR001 = {};


USR001.loadDataTable = function(isSearch) {
    
        $('#dataTable')
	    .dataTable(
		    {
			bDestroy : true,
			bProcessing : false,
			bServerSide : true, 
			bLengthChange : false,
			bFilter : false,
//			"bJQueryUI": true,
			sAjaxSource : "getAllUser",
			fnServerParams : function(aoData) {
			    // --- for search params
			    aoData.push({
				"name" : "user.userName",
				"value" : $('#userName').val()
			    }, {
				"name" : "user.firstName",
				"value" : $('#firstName').val()
			    }, {
				"name" : "user.lastName",
				"value" :$('#lastName').val()
			    }, {
				"name" : "user.email",
				"value" :$('#email').val()
			    }, {
				"name" : "user.role",
				"value" :$('#role').val()
			    }, {
				"name" : "user.active",
				"value" : $('#active').val()
			    });
			},
			columns : [ {
			    "data" : "userName",
			    "sClass" : "center"
			}, {
			    "data" : "userName"
			}, {
			    "data" : "firstName"
			}, {
			    "data" : "lastName"
			}, {
			    "data" : "email"
			}, {
			    "data" : "roles"
			}, {
			    "data" : "active",
			    "sClass" : "center"
			} ],
			aaSorting: [[1, 'asc']],
			columnDefs : [
				{
				    render : function(data, type, row) {
					var rol = [];
					$.each(data, function(a) {
					    rol.push(data[a].roleName);
					});
					return rol.join();
				    },
				   targets : 5
				},
				{
				    render : function(data, type, row) {
					 return  _COMMON.StatusRender[data];
				    },
				    targets : 6
				},
    	                   {
    	                      render: function ( data, type, row ) {
    	                	   return  _COMMON.ButtonRender.editBtn('USR001.editByUserName(\''+data+'\')' ,  _COMMON.isUserHide) + '&nbsp;'+
    	                	   	_COMMON.ButtonRender.delBtn('USR001.onUserDelete(\''+data+'\')',  _COMMON.isUserHide);
    	                       },
    	                       targets: 0
    	                   },
    	                   { 'bSortable': false, 'aTargets': [ 0 ] }
    	               ],
        });
    
};

USR001.clearHeaderCriteria = function(){
    $('.required').unValidate();
    $('#userName').clear();
    $('#firstName').clear();
    $('#lastName').clear();
    $('#role').clear();
    $('#email').clear();
    $('#active').clear();
};


USR001.onUserDelete = function(name) {
//    location = 'USR001A?user.userName='+name;

    $("#confirm").html("Do you want to delete ?");

    $("#confirm").dialog({
        resizable: false,
        modal: true,
        title: "Confirm Delete",
        height: 180,
        width: 300,
        buttons: {
            "Yes": function () {
                $(this).dialog('close');
                
                location = 'USR001Adelete?user.userName='+name;
                
            },
                "No": function () {
                $(this).dialog('close');
            }
        }
    });
};

USR001.editByUserName = function(name) {
    location = 'USR001A?user.userName='+name;
};

USR001.init = function() {
//	$("#btnSave").show();
//	$("#btnUpdate").hide();
};


$(document).ready(function() {    
    
    USR001.loadDataTable();

    $("#btnSearch").click(function() {
    	$('.required').unValidate();
    	USR001.loadDataTable(true);
    });
    
    $("#btnClear").click(function() {
	USR001.clearHeaderCriteria();
    });
    
    _COMBOBOX.StatusCombo('active');
    
});
